package com.recommend.recommendkeyword.controller;

import com.recommend.recommendkeyword.common.CommonResponse;
import com.recommend.recommendkeyword.service.SearchService;
import com.recommend.recommendkeyword.vo.RecommendKeywordVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/search")
public class SearchController {
    @Autowired
    SearchService searchService;

    /**
     * 竞争词，中介词搜索（竞争度降序排序）
     */
    @GetMapping("/competition")
    public CommonResponse<RecommendKeywordVO> searchByComp(@RequestParam String seedKeyword, @RequestParam int searchUserId){
        RecommendKeywordVO recommendKeywordVO = searchService.searchByComp(seedKeyword, searchUserId);
        if(recommendKeywordVO == null)
            return CommonResponse.createForError("搜索失败");
        return CommonResponse.createForSuccess("搜索成功", recommendKeywordVO);
    }

    /**
     * 用户查看近期搜索记录
     */
    @GetMapping("/user")
    public CommonResponse<List<RecommendKeywordVO>> searchHistory(@RequestParam int userId){
        List<RecommendKeywordVO> recommendKeywordVOList = searchService.searchHistory(userId);
        if(recommendKeywordVOList == null)
            return CommonResponse.createForError("搜索失败");
        return CommonResponse.createForSuccess("搜索成功", recommendKeywordVOList);
    }
}
